package com.freak.wanandroid.model.classify;

import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.freak.wanandroid.R;
import com.freak.wanandroid.base.fragment.BaseLifecycleFragment;
import com.freak.wanandroid.bean.ClassifyBean;
import com.freak.wanandroid.model.classify.activity.ClassifyDetailActivity;
import com.freak.wanandroid.model.classify.adapter.ClassifyDetailAdapter;
import com.freak.wanandroid.model.login.LoginActivity;
import com.freak.wanandroid.net.CommonLiveData;
import com.freak.wanandroid.util.ToastUtil;
import com.freak.wanandroid.util.ToolUtils;
import com.freak.wanandroid.view.pullrefreshview.layout.PullRefreshLayout;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class DetailFragment extends BaseLifecycleFragment<ClassifyViewModel> {
    private XRecyclerView mClassifyRecycle;
    private ClassifyDetailAdapter mClassifyDetailAdapter;
    private List<ClassifyBean.Datas> mList;
    private int mPage = 1;
    private String mCid = "";
    private PullRefreshLayout mClassifyIncludeNoData;


//    /**
//     * 断网重连
//     * @param networkType
//     */
//    public void onNetConnected(NetworkType networkType) {
//        super.onNetConnected(networkType);
//        page = 1;
//        mViewModel.getClassify(String.valueOf(page), cid);
//    }

    public DetailFragment(String cid) {
        this.mCid = cid;
    }

    @Override
    protected void dataObserver() {
        mViewModel.mGetClassify.observe(this, this::getClassifySuccess);
        mViewModel.mDoCollect.observe(this, this::doCollectSuccessOrError);
        mViewModel.mDoCancelCollect.observe(this, this::doCancelCollectSuccessOrError);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_classify_detail;
    }

    @Override
    protected void initEventAndData(View view) {
        mList = new ArrayList<>();
        mClassifyRecycle = view.findViewById(R.id.classify_recycle);
        mClassifyIncludeNoData = view.findViewById(R.id.classify_include_no_data);
        mClassifyRecycle.setLayoutManager(new LinearLayoutManager(getActivity()));
        mClassifyRecycle.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPage = 1;
                mViewModel.getClassify(String.valueOf(mPage), mCid);
                showLoading();
            }

            @Override
            public void onLoadMore() {
                mPage++;
                mViewModel.getClassify(String.valueOf(mPage), mCid);
                showLoading();
            }
        });
        mClassifyRecycle.setFootViewText("正在努力加载中...", "我也是有底线的");
    }


    protected void onResumeLoadData() {
        mPage = 1;
        mViewModel.getClassify(String.valueOf(mPage), mCid);
        showLoading();
    }


    public void getClassifySuccess(ClassifyBean model) {
        dismissLoading();
        List<ClassifyBean.Datas> mDataList = model.getDatas();
        if (mPage == 1) {
            refresh(mDataList);
        } else {
            loading(mDataList);
        }
    }

    public void refresh(List<ClassifyBean.Datas> list) {
        if (list != null && list.size() != 0) {
            mClassifyIncludeNoData.setVisibility(View.GONE);
            mClassifyRecycle.setVisibility(View.VISIBLE);
            mList.clear();
            mList.addAll(list);
            if (mClassifyDetailAdapter == null) {
                mClassifyDetailAdapter = new ClassifyDetailAdapter(mList, getActivity());
                mClassifyRecycle.setAdapter(mClassifyDetailAdapter);
                mClassifyDetailAdapter.setmOnItemClickListener(new ClassifyDetailAdapter.OnItemClickListener() {
                    @Override
                    public void doCollage(ClassifyBean.Datas mData, ImageView imageView) {
                        if (mData.getCollect()) {
                            mViewModel.doCancelCollect(mData.getId());
                        } else {
                            mViewModel.doCollect(mData.getId());
                        }
                        showLoading();
                    }

                    @Override
                    public void doIntern(ClassifyBean.Datas mData) {
                        Intent intent = new Intent(getActivity(), ClassifyDetailActivity.class);
                        intent.putExtra("url", mData.getLink());
                        intent.putExtra("title", mData.getTitle());
                        startActivity(intent);
                    }
                });
            } else {
                mClassifyDetailAdapter.setData(mList);
                mClassifyDetailAdapter.notifyDataSetChanged();
            }
            mClassifyRecycle.refreshComplete();
        } else {
            mClassifyRecycle.setVisibility(View.GONE);
            mClassifyIncludeNoData.setVisibility(View.VISIBLE);
        }
    }

    public void loading(List<ClassifyBean.Datas> list) {
        mClassifyRecycle.loadMoreComplete();
        //加载下一页
        if (list != null && list.size() != 0) {
            mList.addAll(list);
            mClassifyDetailAdapter.notifyDataSetChanged();
        } else {
            mClassifyRecycle.setNoMore(true);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mClassifyRecycle.setNoMore(false);//已经全部加载完成
                }
            }, 1000);
        }
    }

    /**
     * 收藏成功或失败处理
     *
     * @param commonLiveData CommonLiveData
     */
    public void doCollectSuccessOrError(CommonLiveData commonLiveData) {
        dismissLoading();
        if (CommonLiveData.SUCCESS == commonLiveData.code) {
            ToastUtil.showShort(getActivity(), "收藏成功");
            return;
        }
        ToastUtil.showShort(getActivity(), "登录过期，请重新登录");
        ToolUtils.logout(getActivity());
        LoginActivity.startAction(getActivity());
    }

    public void doCancelCollectSuccessOrError(CommonLiveData commonLiveData) {
        dismissLoading();
        if (CommonLiveData.SUCCESS == commonLiveData.code) {
            ToastUtil.showShort(getActivity(), "取消收藏成功");
            return;
        }
        ToastUtil.showShort(getActivity(), "登录过期，请重新登录");
        ToolUtils.logout(getActivity());
        LoginActivity.startAction(getActivity());
    }
}
